const router = require('koa-router')()
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const models = require('../models/index')
const moment = require('moment')
const md5 = require('md5')

router.post('/admin_list', async ctx => {
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    let user = await models.user.findAndCountAll({
        where: {
            user_id: {
                // 查找id大于1
                [Op.gt]: 1
            }
        },
        limit: pageshow,
        offset: (page - 1)*pageshow
    })
    return ctx.body = {
        code: 200,
        data: {
            list: user.rows.map(item => {
                return {
                    user_id: item.user_id,
                    username: item.username,
                    lock: item.lock,
                    auth: item.auth,
                    updated_at: moment(item.updated_at*1000).format('YYYY-MM-DD HH:mm:ss')
                }
            }),
            count: user.count
        },
        msg: '操作成功'
    }
})

router.post('/admin_operation', async ctx => {
    let type = ctx.request.body.type
    let role = ctx.request.body.role
    let user_id = ctx.request.body.user_id
    let status = ctx.request.body.status
    let username = ctx.request.body.username
    let password = ctx.request.body.password
    
    if (type == 'add') {
        // 新增管理员
        await models.user.findOrCreate({
            where: {
                username: username
            }, 
            defaults: {
                lock: '1',
                auth: role,
                password: md5(password),
                created_at: Date.parse(new Date) / 1000,
                updated_at: Date.parse(new Date) / 1000
            }
        })
        .then(([user, created]) => {
            if (created) {
                return ctx.body = {
                    code: 200,
                    data: {},
                    msg: '操作成功'
                }
            } else {
                return ctx.body = {
                    code: 0,
                    data: {},
                    msg: '账号已存在'
                }  
            }
        })
    } else if (type == 'save') {
        // 编辑管理员
        await models.user.update({
            username,
            password: md5(password),
            auth: role,
            updated_at: Date.parse(new Date) / 1000
        }, {
            where: {
                user_id
            }
        })
        return ctx.body = {
            code: 200,
            data: {},
            msg: '操作成功'
        }
    } else if (type == 'status') {
        if (status == 0) {
            // 封禁
            await models.user.update({
                lock: '0',
                updated_at: Date.parse(new Date) / 1000
            }, {
                where: {
                    user_id
                }
            })
            return ctx.body = {
                code: 200,
                data: {},
                msg: '操作成功'
            }
        } else if (status == 1) {
            // 开启
            await models.user.update({
                lock: '1',
                updated_at: Date.parse(new Date) / 1000
            }, {
                where: {
                    user_id
                }
            })
            return ctx.body = {
                code: 200,
                data: {},
                msg: '操作成功'
            }
        }
    } else if (type == 'del') {
        // 删除管理员
        await models.user.destroy({
            where: {
                user_id
            }
        })
        return ctx.body = {
            code: 200,
            data: {},
            msg: '操作成功'
        }
    }
})

module.exports = router